# Copyright (C) 2025 Kumo inc.
# Author: Jeff.li lijippy@163.com
# All rights reserved.
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <https:#www.gnu.org/licenses/>.
#

"""Tests for converter.py."""

import logging

from turbo import logging as turbo_logging
from turbo.logging import converter
from turbo.testing import turbotest


class ConverterTest(turbotest.TestCase):
  """Tests the converter module."""

  def test_turbo_to_cpp(self):
    self.assertEqual(0, converter.turbo_to_cpp(turbo_logging.DEBUG))
    self.assertEqual(0, converter.turbo_to_cpp(turbo_logging.INFO))
    self.assertEqual(1, converter.turbo_to_cpp(turbo_logging.WARN))
    self.assertEqual(2, converter.turbo_to_cpp(turbo_logging.ERROR))
    self.assertEqual(3, converter.turbo_to_cpp(turbo_logging.FATAL))

    with self.assertRaises(TypeError):
      converter.turbo_to_cpp('')

  def test_turbo_to_standard(self):
    self.assertEqual(
        logging.DEBUG, converter.turbo_to_standard(turbo_logging.DEBUG))
    self.assertEqual(
        logging.INFO, converter.turbo_to_standard(turbo_logging.INFO))
    self.assertEqual(
        logging.WARNING, converter.turbo_to_standard(turbo_logging.WARN))
    self.assertEqual(
        logging.WARN, converter.turbo_to_standard(turbo_logging.WARN))
    self.assertEqual(
        logging.ERROR, converter.turbo_to_standard(turbo_logging.ERROR))
    self.assertEqual(
        logging.FATAL, converter.turbo_to_standard(turbo_logging.FATAL))
    self.assertEqual(
        logging.CRITICAL, converter.turbo_to_standard(turbo_logging.FATAL))
    # vlog levels.
    self.assertEqual(9, converter.turbo_to_standard(2))
    self.assertEqual(8, converter.turbo_to_standard(3))

    with self.assertRaises(TypeError):
      converter.turbo_to_standard('')

  def test_standard_to_turbo(self):
    self.assertEqual(
        turbo_logging.DEBUG, converter.standard_to_turbo(logging.DEBUG))
    self.assertEqual(
        turbo_logging.INFO, converter.standard_to_turbo(logging.INFO))
    self.assertEqual(
        turbo_logging.WARN, converter.standard_to_turbo(logging.WARN))
    self.assertEqual(
        turbo_logging.WARN, converter.standard_to_turbo(logging.WARNING))
    self.assertEqual(
        turbo_logging.ERROR, converter.standard_to_turbo(logging.ERROR))
    self.assertEqual(
        turbo_logging.FATAL, converter.standard_to_turbo(logging.FATAL))
    self.assertEqual(
        turbo_logging.FATAL, converter.standard_to_turbo(logging.CRITICAL))
    # vlog levels.
    self.assertEqual(2, converter.standard_to_turbo(logging.DEBUG - 1))
    self.assertEqual(3, converter.standard_to_turbo(logging.DEBUG - 2))

    with self.assertRaises(TypeError):
      converter.standard_to_turbo('')

  def test_standard_to_cpp(self):
    self.assertEqual(0, converter.standard_to_cpp(logging.DEBUG))
    self.assertEqual(0, converter.standard_to_cpp(logging.INFO))
    self.assertEqual(1, converter.standard_to_cpp(logging.WARN))
    self.assertEqual(1, converter.standard_to_cpp(logging.WARNING))
    self.assertEqual(2, converter.standard_to_cpp(logging.ERROR))
    self.assertEqual(3, converter.standard_to_cpp(logging.FATAL))
    self.assertEqual(3, converter.standard_to_cpp(logging.CRITICAL))

    with self.assertRaises(TypeError):
      converter.standard_to_cpp('')

  def test_get_initial_for_level(self):
    self.assertEqual('F', converter.get_initial_for_level(logging.CRITICAL))
    self.assertEqual('E', converter.get_initial_for_level(logging.ERROR))
    self.assertEqual('W', converter.get_initial_for_level(logging.WARNING))
    self.assertEqual('I', converter.get_initial_for_level(logging.INFO))
    self.assertEqual('I', converter.get_initial_for_level(logging.DEBUG))
    self.assertEqual('I', converter.get_initial_for_level(logging.NOTSET))

    self.assertEqual('F', converter.get_initial_for_level(51))
    self.assertEqual('E', converter.get_initial_for_level(49))
    self.assertEqual('E', converter.get_initial_for_level(41))
    self.assertEqual('W', converter.get_initial_for_level(39))
    self.assertEqual('W', converter.get_initial_for_level(31))
    self.assertEqual('I', converter.get_initial_for_level(29))
    self.assertEqual('I', converter.get_initial_for_level(21))
    self.assertEqual('I', converter.get_initial_for_level(19))
    self.assertEqual('I', converter.get_initial_for_level(11))
    self.assertEqual('I', converter.get_initial_for_level(9))
    self.assertEqual('I', converter.get_initial_for_level(1))
    self.assertEqual('I', converter.get_initial_for_level(-1))

  def test_string_to_standard(self):
    self.assertEqual(logging.DEBUG, converter.string_to_standard('debug'))
    self.assertEqual(logging.INFO, converter.string_to_standard('info'))
    self.assertEqual(logging.WARNING, converter.string_to_standard('warn'))
    self.assertEqual(logging.WARNING, converter.string_to_standard('warning'))
    self.assertEqual(logging.ERROR, converter.string_to_standard('error'))
    self.assertEqual(logging.CRITICAL, converter.string_to_standard('fatal'))

    self.assertEqual(logging.DEBUG, converter.string_to_standard('DEBUG'))
    self.assertEqual(logging.INFO, converter.string_to_standard('INFO'))
    self.assertEqual(logging.WARNING, converter.string_to_standard('WARN'))
    self.assertEqual(logging.WARNING, converter.string_to_standard('WARNING'))
    self.assertEqual(logging.ERROR, converter.string_to_standard('ERROR'))
    self.assertEqual(logging.CRITICAL, converter.string_to_standard('FATAL'))


if __name__ == '__main__':
  turbotest.main()
